<template>
  <div class="container">
    <MyHeader></MyHeader>
    <el-main>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span v-if="commentList.post">{{ commentList.post.topic }}</span>
          <div>
            <el-button type="text" class="button">校园社区平台</el-button>
            <time class="time" v-if="commentList.post">{{
              new Date(commentList.post.registerTime).toLocaleString()
            }}</time>
          </div>
        </div>
        <div class="content">
          <el-popover placement="top-start" width="200" trigger="hover">
            <div class="content-user" v-if="commentList.user">
              <!-- 用户头像 -->
              <img
                :src="commentList.user.portrait"
                class="userHeadImg"
                @click="goUserpage"
              />
              <span v-if="commentList.user">{{
                commentList.user.username
              }}</span>
              <span v-if="commentList.user">{{
                commentList.user.introduction
              }}</span>
            </div>
            <div slot="reference" class="content-user" v-if="commentList.user">
              <img
                :src="commentList.user.portrait"
                class="userHeadImg"
                @click="goUserpage"
              />
              <span>{{ commentList.user.username }}</span>
            </div>
          </el-popover>

          <div
            class="text item"
            v-if="commentList.post"
            v-html="commentList.post.text"
          >
            <!-- {{ deleteLabel(commentList.post.text) }} -->
          </div>

          <!-- 点赞收藏区 -->
          <div class="content-main" v-for="(item, i) in comments" :key="i">
            <!-- 点赞量 -->
            <span
              class="content-btns"
              @click="countlikeNumber('1', i, item.id)"
            >
              <i class="iconfont icon-dianzan3"></i>
              {{ item.like }}
            </span>
            <!-- 收藏量 -->
            <span
              class="content-btns"
              @click="countcollectNumber('1', i, item.id)"
            >
              <i class="iconfont icon-shoucang2"></i>
              {{ item.collect }}
            </span>
          </div>
        </div>
        <div>
          <!-- 评论区 -->
          <Comment></Comment>
        </div>
      </el-card>
    </el-main>
  </div>
</template>

<script>
import MyHeader from '../../components/MyHeader.vue'
import Comment from '../../components/detail/comment'
import { post } from '../../api/request.js'
export default {
  name: 'page-detail',
  components: {
    MyHeader,
    Comment
  },
  data () {
    return {
      lastSid: '',
      lastPid: '',
      commentList: {
        pid: '',
        sid: ''
      },
      index: '0',
      subIndex: '0',
      comments: [
        {
          id: '1', // 用户id
          collect: 0, // 评论数
          like: 0, // 点赞数
          isLike: false,
          isCollect: false,
          likeListId: [],
          collectListId: [],
          inputShow: false
        }
      ],
      activeNames: ['1'],
      currentPage4: 1
    }
  },
  created () {
    const loading = this.$loading({
      lock: true,
      text: '数据加载中......',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    })
    setTimeout(() => {
      loading.close()
    }, 1000) /* 设置时间为1秒 */
    this.lastPid = this.$route.query.pid
    this.lastSid = this.$route.query.sid
    this.getcommentsList(this.$route.query.sid, this.$route.query.pid)
  },
  methods: {
    async getcommentsList (sid, pid) {
      this.commentList.pid = pid
      this.commentList.sid = sid
      let data = await post(`section/${sid}/${pid}`, this.commentList)
      if (typeof data === 'string') {
        data = eval('(' + data + ')')
      }
      // console.log(data);
      if (data.post === null) {
        return this.$message.error('加载失败，稍后重试！')
      }
      this.commentList = data
      return this.$message.success('获取帖子详情成功！')
    },
    // 删除标签
    deleteLabel (text) {
      const reg = /<\/?.+?\/?>/g
      return text.replace(reg, '')
    },
    goUserpage () {
      this.$message.success('欢迎来到用户主页！')
      this.$router.push('user')
    },
    handleChange (val) {},
    // 实现点赞收藏回复功能
    addlikeNumber (i, id) {
      const list = this.comments[i].likeListId
      // console.log(i, name, id)
      if (list.length === 0) {
        // 在已经点赞的列表中未找到userId
        this.comments[i].isLike = true
        this.comments[i].like += 1
        this.comments[i].likeListId.push(id)
        console.log(
          '点赞+1',
          this.comments[i].isLike,
          this.comments[i].like,
          this.comments[i].likeListId
        )
      } else {
        const index = list.indexOf(this.myId)
        this.comments[i].isLike = false
        this.comments[i].like -= 1
        this.comments[i].likeListId.splice(index, 1)
        console.log(
          '点赞-1',
          this.comments[i].isLike,
          this.comments[i].likeListId
        )
      }
    },
    countlikeNumber (type, i, j, id) {
      const text = type === '1' ? this.comments[i] : this.comments[i].reply[j]
      const list = text.likeListId
      if (list.length === 0) {
        // 在已经点赞的列表中未找到userId
        text.isLike = true
        text.like += 1
        text.likeListId.push(id)
        console.log('点赞+1', text.isLike, text.like, text.likeListId)
      } else {
        const index = list.indexOf(this.myId)
        text.isLike = false
        text.like -= 1
        text.likeListId.splice(index, 1)
        console.log('点赞-1', text.isLike, text.likeListId)
      }
    },
    // 添加收藏数
    addcollectNum (i, id) {
      const list = this.comments[i].collectListId
      // console.log(i, name, id)
      if (list.length === 0) {
        // 在已经点赞的列表中未找到userId
        this.comments[i].isCollect = true
        this.comments[i].collect += 1
        this.comments[i].collectListId.push(id)
        console.log(
          '收藏+1',
          this.comments[i].isCollect,
          this.comments[i].collect,
          this.comments[i].collectListId
        )
      } else {
        const index = list.indexOf(this.myId)
        this.comments[i].isCollect = false
        this.comments[i].collect -= 1
        this.comments[i].collectListId.splice(index, 1)
        console.log(
          '收藏-1',
          this.comments[i].isCollect,
          this.comments[i].collectListId
        )
      }
    },
    countcollectNumber (type, i, j, id) {
      const text = type === '1' ? this.comments[i] : this.comments[i].reply[j]
      const list = text.collectListId
      if (list.length === 0) {
        // 在已经点赞的列表中未找到userId
        text.isCollect = true
        text.collect += 1
        text.collectListId.push(id)
        console.log('收藏+1', text.isCollect, text.collect, text.collectListId)
      } else {
        const index = list.indexOf(this.myId)
        text.isCollect = false
        text.collect -= 1
        text.collectListId.splice(index, 1)
        console.log('收藏-1', text.isCollect, text.collectListId)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
.box-card {
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%);
  width: 80%;
  height: auto;
}
.text {
  font-size: 16px;
  text-indent: 2rem;
}
.time {
  font-size: 13px;
  color: #999;
}
.button {
  padding: 5px;
}
.item {
  margin-bottom: 18px;
}
.content-user {
  display: flex;
  width: 100px;
  flex-direction: column;
  justify-content: center;
}
.userHeadImg {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  float: left;
}
.content-user span {
  flex: 1;
  font-size: 14px;
  color: #999;
  margin-top: 10px;
  margin-left: 10px;
}
.content-img {
  width: 100%;
  text-align: center;
}
.content-img img {
  height: 400px;
}
.content-main {
  height: 50px;
}
.content-btns {
  float: right;
  margin-right: 40px;
}
.clearfix {
  text-align: center;
  height: 40px;
  font-size: 24px;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>
